﻿CREATE PROCEDURE [dbo].[proc_Customer_Create]
	(
		@CustomerId uniqueidentifier,
		@CustomerName nvarchar(200),
		@Project nvarchar(200),
		@WebAddress nvarchar(100),
		@Tel nvarchar(50),
		@Fax nvarchar(50),
		@pid int,
		@cid int,
		@Rid int,
		@Address nvarchar(200),
		@Zip nvarchar(20),
		@IndustryOneId int,
		@IndustryTwoId int,
		@SourceId int,
		@TypeId int,
		@StateId int,		
		@ProtectId int,
		@CreateName nvarchar(50),
		@CreateDate datetime,
		@MnemonicWorks nvarchar(50),
		@SalesPerson nvarchar(20),
		@ServicePerson nvarchar(20),
		@CompanyId int,		
		@ZName nvarchar(50),
		@Sex nvarchar(10),
		@Position nvarchar(20),
		@OfficeTel nvarchar(50),
		@HomeTel nvarchar(50),
		@Mobile nvarchar(50),
		@Email nvarchar(50),
		@IsMain bit,
		@PersonTypeId int		
	)
AS
Begin
	Declare @nId int
	Set @nId = 0
	Select @nId = Count(0) From Customer Where CompanyId=@CompanyId And CustomerName=@CustomerName And Audit=0
	If @nId > 0		--该客户数据已经录入，正在等待审核 By 赵洪涛 2012-08-16
	Begin
		Select -1 ReturnValue
		Return -1
	End

--2012-12-12 去掉限制
	--该公司未审核客户总数
--	Select @nId = Count(0) From Customer Where CompanyId=@CompanyId And Audit=0
--	If @nId>50 And @CompanyId<>1	--未审核的客户数超过50，且不是山西公司
--	Begin
--		Select -2 ReturnValue
--		Return -2
--	End

	declare @ProtectName nvarchar(50)
	declare @ProtectDate datetime
	if (@ProtectId = 1)
		begin
			set @ProtectName = null
			set @ProtectDate = null
		end
	else
		begin
			set @ProtectName = @CreateName
			set @ProtectDate = @CreateDate
		end

	Insert Into Customer(CompanyId,CustomerId,CustomerName,Project,WebAddress,ZName,Tel,Fax,Address,Zip,IndustryOneId,IndustryTwoId,SourceId,TypeId,StateId,ProtectId,pid,cid,Rid,CreateName,CreateDate,ProtectName,ProtectDate,UpTime,MnemonicWorks,SalesPerson,ServicePerson) Values
	(@CompanyId,@CustomerId,@CustomerName,@Project,@WebAddress,@ZName,@Tel,@Fax,@Address,@Zip,@IndustryOneId,@IndustryTwoId,@SourceId,@TypeId,@StateId,@ProtectId,@pid,@cid,@Rid,@CreateName,@CreateDate,@ProtectName,@ProtectDate,@CreateDate,@MnemonicWorks,@SalesPerson,@ServicePerson)
	
	Insert Into ContactPerson(CustomerId,ZName,Sex,[Position],OfficeTel,HomeTel,Mobile,Email,IsMain,PersonTypeId) Values
	(@CustomerId,@ZName,@Sex,@Position,@OfficeTel,@HomeTel,@Mobile,@Email,@IsMain,@PersonTypeId)

	Begin
		Select 1 ReturnValue
		Return 1
	End
End